/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	AMeterServiceImpl.java
 * Created：		2016年8月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.meter;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.common.AQueryCommonParam;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.AUserGroupQueryParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.common.SUserGroupQueryParamConverter;
import com.thor.palmmall.rest.complaint.converter.SComplaintQueryParamConverter;
import com.thor.palmmall.rest.meter.converter.AMeterConverter;
import com.thor.palmmall.rest.meter.converter.AMeterInputConverter;
import com.thor.palmmall.rest.meter.converter.SMeterInputConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.meter.SMeter;
import com.thor.palmmall.spi.meter.SMeterInput;
import com.thor.palmmall.spi.meter.SMeterService;

/**
 * @author wolv
 *
 */
public class AMeterServiceImpl implements AMeterService {

  @Override
  public List<AMeter> queryMeters(String storeUuid, String serialNumber, String categoryCode)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(storeUuid, "storeUuid");
    SResponse<List<SMeter>> resp = sBasicService.queryMeters(storeUuid,serialNumber,categoryCode);
    if (resp.isSuccess()){
      return ConverterUtil.convert(resp.getBody(), AMeterConverter.getInstance());
    } else{
      throw new ThorServiceException("无法查询仪表：" + resp.getMessage());
    }
  }

  @Override
  public AMeter getMeter(String uuid) throws ThorServiceException {
    Assert.assertArgumentNotNull(uuid, "uuid");

    SResponse<SMeter> resp = sBasicService.getMeter(uuid);
    if (resp.isSuccess())
      return AMeterConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法查询仪表：" + resp.getMessage());
  }

  @Override
  public String create(AMeterInput meterInput, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(meterInput, "meterInput");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sMeterService.create(
        SMeterInputConverter.getInstance().convert(meterInput),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建仪表录入单：" + resp.getMessage());
  }

  @Override
  public AMeterInput get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SMeterInput> resp = sMeterService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return AMeterInputConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的仪表录入单：" + resp.getMessage());
  }

  @Override
  public QueryResult<AMeterInput> query(AQueryCommonParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(param, "param");

    SResponse<SQueryResult<SMeterInput>> resp = sMeterService
        .query(SComplaintQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), AMeterInputConverter.getInstance());
    else
      throw new ThorServiceException("无法查询仪表录入单：" + resp.getMessage());
  }

  @Autowired
  private SMeterService sMeterService;
  @Autowired
  private SBasicService sBasicService;

}
